<template>
	<div class="app-container">
		<!-- 添加搜索 -->
		<div class="filter-container tw-filter">
			<el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-delete" v-has="$store.state.user.btn_common+'del'" @click="handleDelete()">批量删除</el-button>
			<el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-delete" v-has="$store.state.user.btn_common+'del'" @click="handleDeleteSix()">删除6个月以前的</el-button>

      <el-input v-model="queryList.admin_id" placeholder="请输入管理员ID" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.admin_real_name" placeholder="请输入管理员姓名" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.name" placeholder="请输入操作页面" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.desc" placeholder="请输入操作内容" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.url" placeholder="请输入操作URL" class="filter-item tw-filter-input" clearable />
      <el-input v-model="queryList.ip" placeholder="请输入操作IP" class="filter-item tw-filter-input" clearable />
      <el-date-picker @change="changeData" value-format="yyyy-MM-dd" v-model="add_time_scope" type="daterange" unlink-panels :picker-options="pickerOptions"
      start-placeholder="开始日期" end-placeholder="结束日期" class="filter-item tw-filter-input-mr" clearable></el-date-picker>

			<el-button v-waves class="filter-item tw-filter-button" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
		</div>

		<!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border fit highlight-current-row style="width: 100%;"
      >
			<el-table-column type="selection" width="50" />
      <el-table-column label="管理员ID" prop="admin_id" align="center" width="80" />
			<el-table-column label="管理员姓名" prop="admin_real_name" align="center" />
			<el-table-column label="页面名称" prop="name" align="left" />
      <el-table-column label="操作内容" prop="desc" align="left" />
      <el-table-column label="操作URL" prop="url" align="left" />
      <el-table-column label="操作IP" prop="ip" align="center" />
      <el-table-column label="操作时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.add_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</span>
        </template>
      </el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="{row}">
					<el-button size="mini" v-has="$store.state.user.btn_common+'del'" @click="handleDelete(row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="isPage && listTotal > 0" :total="listTotal" :page.sync="queryList.curpage" :limit.sync="queryList.page" @pagination="getList" />
	</div>
</template>

<script>
	import Pagination from '@/components/Pagination' // 基于el分页的二次封装
	import waves from '@/directive/waves' // waves directive

	export default {
		components: { Pagination },
		directives: { waves },
		data() {
			return {
        // 数据列表
        listLoading: true,
        listTotal: 0,
        isPage: false,
        tableList: [],
        multipleSelection: [],//多选数据

        //键字段
        keyName: 'id',
        apiPath: 'admin.log',

				// 搜索数据
				queryList: {
					page: this.$store.state.settings.page, // 每页显示数量
					curpage: 1, // 当前页码
					admin_id: '', //管理员ID
					admin_real_name: '', //员管理员姓名
					name:'',  //页面名称
					desc:'',  //操作内容
					url:'',
					ip:'', //IP
					add_time_scope:'', //时间范围
				},
        add_time_scope: [],

        //带快捷选项时间
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        }
			}
		},
		created() {
			//获取数据列表
		  this.getList();
		},
		methods: {
      // 搜索
      handleFilter() {
      	this.queryList.curpage = 1
      	this.tableList = []
        this.listTotal = 0
      	this.getList()
      },
      // 列表选择
      handleSelectionChange(val) {
      	this.multipleSelection = val
      },
      // 获取数据列表
      getList() {
      	this.listLoading = true
      	this.$api.fetchList(this.queryList, this.apiPath).then(response => {
      		this.tableList = response.list
      		this.listTotal = response.list_count
      		this.isPage = response.is_page
          this.listLoading = false
      	})
      },
			// 删除
			handleDelete(id = 0) {
			  let current = this
			  let id_str = ''
			  if (!id) {
			    // 多选
			    if (!this.multipleSelection.length) {
			      this.$message({
			        message: '请选择删除的条目！',
			        type: 'warning'
			      })
			      return false
			    }
			    // 数据处理
			    this.multipleSelection.forEach(item => {
			      id_str += item[current.keyName] + ','
			    })
			  } else {
			    id_str = id
			  }

			  // 提交数据
			  this.$confirm('确认删除数据吗?', '提示', {
			    confirmButtonText: '确定',
			    cancelButtonText: '取消',
			    type: 'warning'
			  }).then(() => {
			    this.$api.del({id_str: id_str}, this.apiPath).then(response => {
			      // 重新获取列表
			      this.getList()
			      this.$message({
			        message: '删除成功',
			        type: 'success'
			      })
			    })
			  })
			},
			// 删除6个月以前的
			handleDeleteSix() {
				// 提交数据
				this.$confirm('确认删除数据吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api.del({is_ago: 1}, this.apiPath).then(response => {
						// 重新获取列表
						this.getList()
						this.$message({
							message: '删除成功',
							type: 'success'
						})
					})
				})
			},
			//选择搜索时间范围
			changeData(val) {
				this.queryList.add_time_scope = val[0] + ' - ' + val[1];
			}
		}
	}
</script>
